﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ICIA.ARojas.Visual.Persona.ViewModel;
using ICIA.ARojas.Visual.Destino;
using System.Windows.Media.Imaging;
using System.IO;

namespace ICIA.ARojas.Visual.Persona
{
    public partial class MainPage : UserControl
    {
        VmPersona viewmodel;

        public MainPage()
        {
            InitializeComponent();
            viewmodel = new VmPersona();
            this.DataContext = viewmodel;

            viewmodel.ACSBGrado = ACSBGrado.viewmodel;
            viewmodel.ACSBEspecialidad = ACSBEspecialidad.viewmodel;

            gridPersonas.SelectionChanged += new SelectionChangedEventHandler(gridPersonas_SelectionChanged);

            txtBuscador.KeyUp += new KeyEventHandler(txtBuscador_KeyUp);
        }

        void txtBuscador_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key != Key.Enter)
            {
                viewmodel.BuscarPersonasEnDataGrid(txtBuscador.Text);
            
            }
        }

        void gridPersonas_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            if (viewmodel.SelectedItem != null)
            {
               
                try
                {
                    var image = new BitmapImage { UriSource = new Uri(viewmodel.SelectedItem.GUID, UriKind.Relative) };
                    fotoPersonal.Source = image;                    
                }
                catch (Exception ex) { }
            }

        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog objOpenFileDialog = new OpenFileDialog();
            objOpenFileDialog.Filter = @"JPEG Files (*.jpg;*.jpeg)|*.jpg;*.jpeg|
                                         PNG Files (*.png)|*.png|All Files (*.*)|*.*";
            
            if ((bool)objOpenFileDialog.ShowDialog())
            {

                string archivo = objOpenFileDialog.File.Name;
                if (viewmodel.SelectedItem != null)
                    viewmodel.SelectedItem.GUID = archivo;                

                Stream stream = objOpenFileDialog.File.OpenRead();
                BitmapImage bi = new BitmapImage();
                bi.SetSource(stream);
                fotoPersonal.Source = bi;
                stream.Close();
            }

        }
    }
}
